﻿using Edge.Repository.Model.CameraSys;
using Edge.Repository.Model.CameraSys.CDal;
using HYRN.Common.Style;
using Oliving.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using ZedGraph;
using HYRN.Common.ConvertUtilities;
using System.Runtime.InteropServices;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using HZH_Controls.Controls;
using System.IO;
using System.Threading;

namespace CameraClient.CForm.CameraF
{
    public partial class FormChart : Form
    {
        CameraConfigModel _cameraConfig = new CameraConfigModel();
        public FormChart(CameraConfigModel CameraConfig)
        {
            InitializeComponent();
            _cameraConfig = CameraConfig;

            this.Text = CameraConfig.Name;
        }

        public int PointCount = 200;

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dateTimeA = (dateTimePicker1.Value.ToString("yyyy-MM-dd") + " " + dateTimePicker2.Value.ToString("HH:mm:ss")).ObjToDate();
            DateTime dateTimeB = (dateTimePicker4.Value.ToString("yyyy-MM-dd") + " " + dateTimePicker3.Value.ToString("HH:mm:ss")).ObjToDate();

            DateTime beginTime = dateTimeA;
            DateTime endTime = dateTimeB;


            int maxWenDu = _cameraConfig.MaxWenDu.ObjToInt(); ;
            int minWenDu = _cameraConfig.MinWenDu.ObjToInt();
             

            CameraDataService cameraDal = new CameraDataService();


            int listcount = cameraDal.Query(F =>
               (F.Pid == _cameraConfig.Id) &&
                 F.CreateTime >= dateTimeA && F.CreateTime < dateTimeB).Count();


            List<CameraDataModel> list = new List<CameraDataModel>();
            if (listcount <= PointCount)
            {
                list = cameraDal.Query(F =>
                 (F.Pid == _cameraConfig.Id) &&
                   F.CreateTime >= dateTimeA && F.CreateTime < dateTimeB)
               .OrderBy(F => F.Id).ToList();
            }
            else
            {
                int jiange = listcount / PointCount;
                list = cameraDal.GetList(dateTimeA, dateTimeB, _cameraConfig.Id, jiange);
            }


            List<DateTime> xTimes = new List<DateTime>();
            string shijian = "";

            //foreach (CameraDataModel item in list)
            //{

            //    xTimes.Add(item.CreateTime);
            //    if (string.IsNullOrEmpty(shijian)) { shijian = "'" + item.CreateTime.ToString() + "'"; }
            //    else { shijian = shijian + ",'" + item.CreateTime.ToString() + "'"; }

            //}

            //for (int i = pointCount + 1; i >= 0; i--)
            //{
            //    DateTime tiemTime = endTime.AddMinutes(-i * jianGeShiChang);
            //    xTimes.Add(tiemTime);

            //    if (string.IsNullOrEmpty(shijian)) { shijian = "'" + tiemTime.ToString() + "'"; } else { shijian = shijian + ",'" + tiemTime.ToString() + "'"; }

            //}

            string xianTitle = "";

            List<CChartShowData> cChartShowDatas = new List<CChartShowData>();



            {

                xianTitle = "'" + _cameraConfig.Name.ToString() + "'";


                List<CameraDataModel> ones = list;

                List<string> wenduOnes = new List<string>();
                //foreach (DateTime t in xTimes)
                //{
                //    List<CameraDataModel> onesWhere = ones.FindAll(F => F.CreateTime == t);
                //    double wenduOne = 0;
                //    if (onesWhere != null && onesWhere.Count > 0)
                //    {
                //        wenduOne = onesWhere.Select(F => F.WenDu).Average();
                //    }
                //    wenduOnes.Add(wenduOne.ObjToInt() == 0 ? "'-'" : wenduOne.ObjToInt().ObjToString());
                //}

                foreach (CameraDataModel item in list)
                {

                    if (string.IsNullOrEmpty(shijian)) { shijian = "'" + item.CreateTime.ToString() + "'"; }
                    else { shijian = shijian + ",'" + item.CreateTime.ToString() + "'"; }


                    wenduOnes.Add(item.WenDu.ObjToInt() == 0 ? "'-'" : item.WenDu.ObjToInt().ObjToString());



                    if (maxWenDu < item.WenDu.ObjToInt())
                    {
                        maxWenDu = item.WenDu.ObjToInt();
                    }



                    if (minWenDu > item.WenDu.ObjToInt())
                    {
                        minWenDu = item.WenDu.ObjToInt();
                    }


                }




                cChartShowDatas.Add(new CChartShowData()
                {
                    Name = _cameraConfig.Name,
                    WenduOnes = wenduOnes
                });

            }


   
            CChartShowDto showDto = CChartHelper.GetShowQuXianData(cChartShowDatas);

            showDto.Xzhou = shijian;
            showDto.YMax = maxWenDu.ToString();
            showDto.YMin = minWenDu.ToString();

            ShowHtml(showDto);

        }



        public void ShowHtml(CChartShowDto showDto)
        {


            #region MyRegion


            string myPath = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "line-all.html");

            string myPathNew = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "html");

            if (!Directory.Exists(myPathNew))
            {
                Directory.CreateDirectory(myPathNew);

            }
            myPathNew = Path.Combine(myPathNew, "line_" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".html");


            string content = File.ReadAllText(myPath);

            //  xAxisList: ['14:00', '15:00', '16:00', '17:00', '18:00'],
            content = content.Replace("{&Xzhou&}", showDto.Xzhou);
            //  legend: ['温度', '温度1'],
            content = content.Replace("{&xianTitle&}", showDto.QuXianTitle);
            content = content.Replace("{&xianseries&}", showDto.QuXianSeries);

            content = content.Replace("{&ymax&}", showDto.YMax);
            content = content.Replace("{&ymin&}", showDto.YMin);


            using (StreamWriter sw = new StreamWriter(myPathNew))
            {
                sw.Write(content);
            }

            //
            // webView2.Url = myPathNew;
            Thread.Sleep(100);


            System.Diagnostics.Process.Start("explorer.exe", myPathNew);

            #endregion




        }



        public void test()
        {

            {


                CameraDataService cameraDal = new CameraDataService();



                DateTime dateTimeA = (dateTimePicker1.Value.ToString("yyyy-MM-dd") + " " + dateTimePicker2.Value.ToString("HH:mm:ss")).ObjToDate();
                DateTime dateTimeB = (dateTimePicker4.Value.ToString("yyyy-MM-dd") + " " + dateTimePicker3.Value.ToString("HH:mm:ss")).ObjToDate();




                List<CameraDataModel> list = cameraDal.Query(F =>
                F.Pid == _cameraConfig.Id &&
                F.CreateTime >= dateTimeA && F.CreateTime < dateTimeB)
                   .OrderBy(F => F.Id).ToList();


                string shijian = "";
                string wnedu = "";
                int index = 0;
                foreach (CameraDataModel item in list)
                {
                    if (string.IsNullOrEmpty(shijian)) { shijian = "'" + item.CreateTime + "'"; } else { shijian = shijian + ",'" + item.CreateTime + "'"; }
                    if (string.IsNullOrEmpty(wnedu)) { wnedu = item.WenDu.ToString(); } else { wnedu = wnedu + "," + item.WenDu + ""; }

                    index = index + 1;

                    // if (index > 10) { break; }
                }

                string myPath = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "line-stack.html");

                string myPathNew = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "html");

                if (!Directory.Exists(myPathNew))
                {
                    Directory.CreateDirectory(myPathNew);
                }
                myPathNew = Path.Combine(myPathNew, "line-stack_" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".html");

                string content = File.ReadAllText(myPath);

                content = content.Replace("{&Xzhou&}", shijian);
                content = content.Replace("{&xianTitle&}", _cameraConfig.Name);
                content = content.Replace("{&wenDus&}", wnedu);
                content = content.Replace("{&ymax&}", _cameraConfig.MaxWenDu.ToString());
                content = content.Replace("{&ymin&}", _cameraConfig.MinWenDu.ToString());


                using (StreamWriter sw = new StreamWriter(myPathNew))
                {
                    sw.Write(content);
                }

                // webView2.Url = myPathNew; 
                //  chromiumWebBrowser1.LoadUrl(myPathNew);
            }

        }

        public void aaa(List<CameraDataModel> list)
        {
            // GraphPane
            if (false)
            {

                #region Zbase

                this.z1.GraphPane.Title.Text = ""; //米字测温折线图
                this.z1.GraphPane.XAxis.Title.Text = "时间";
                this.z1.GraphPane.YAxis.Title.Text = "温度";
                this.z1.GraphPane.XAxis.Type = ZedGraph.AxisType.Date;

                this.z1.GraphPane.YAxis.Scale.Max = _cameraConfig.MaxWenDu;
                this.z1.GraphPane.YAxis.Scale.Min = _cameraConfig.MinWenDu;
                // this.z1.GraphPane.YAxis.Scale.MinorStep = 100;


                //this.z1.GraphPane.XAxis.Scale.Min = 120;
                //this.z1.GraphPane.XAxis.Scale.Max = 0;




                z1.GraphPane.CurveList.Clear();

                #endregion


                #region MyRegion

                #region 定义变量


                PointPairList list_ZhongXin = new PointPairList();

                #endregion

                PointPairList list_ZuiGao = new PointPairList();

                int mycount = 10;
                if (list != null && list.Count > mycount)
                {

                    int fenshu = list.Count / mycount;

                    for (int i = 0; i < mycount; i++)
                    {
                        int index = (i * fenshu);

                        CameraDataModel dto = list[index];

                        list_ZhongXin.Add(new PointPair
                        {
                            X = (double)new XDate(dto.CreateTime),
                            Y = Convert.ToDouble(dto.WenDu),
                            Tag = dto.CreateTime
                        });

                    }
                }
                else
                {

                    foreach (CameraDataModel dto in list)
                    {
                        #region MyRegion

                        // 圆点温度
                        list_ZhongXin.Add(new PointPair
                        {
                            X = (double)new XDate(dto.CreateTime),
                            Y = Convert.ToDouble(dto.WenDu),
                            Tag = dto.CreateTime
                        });

                        #endregion

                    }

                }
                // z1.IsShowPointValues = true;





                #region 添加到试图


                { z1.GraphPane.AddCurve("温度曲线", list_ZhongXin, Color.Blue, SymbolType.None); }

                #endregion





                z1.AxisChange();
                z1.Invalidate();




                #endregion

            }

        }

        private void FormChart_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now;
            dateTimePicker2.Value = DateTime.Now.AddHours(-1);

            dateTimePicker4.Value = DateTime.Now;
            dateTimePicker3.Value = DateTime.Now;

            // button1_Click(null, null);
        }

        private LineItem cursorLine;
        private TextObj cursorText;



    }





}
